import { handleTree } from '@/utils'
import { http } from '@/utils/request'
import type { Menu, MenuParams, RoleMenu, TreeMenu } from './type'

const prefix = import.meta.env.VITE_API_SYS

// 查询菜单列表
export function listMenu(query?: { menuName: string; status: string }) {
	return new Promise(resolve => {
		http.get<any[]>(`${prefix}/menu/list`, query).then(data => {
			const list = handleTree(data.data)
			resolve({ rows: list })
		})
	}) as Promise<{ rows: Menu[] }>
}

// 查询菜单详细
export function getMenu(menuId: number) {
	return http.get<Menu>(`${prefix}/menu/${menuId}`)
}

// 查询菜单下拉树结构
export function treeselect() {
	return http.get<TreeMenu[]>(`${prefix}/menu/treeselect`)
}

// 根据角色ID查询菜单下拉树结构
export function roleMenuTreeselect(roleId: number) {
	return http.get<RoleMenu>(`${prefix}/menu/roleMenuTreeselect/${roleId}`)
}

// 新增菜单
export function addMenu(data: MenuParams) {
	return http.post(`${prefix}/menu`, data)
}

// 修改菜单
export function updateMenu(data: MenuParams) {
	return http.put(`${prefix}/menu`, data)
}

// 删除菜单
export function delMenu(menuId: number) {
	return http.delete(`${prefix}/menu/${menuId}`)
}

export function getRouters(): Promise<Record<string, any>> {
	return http.get(`${prefix}/menu/getRouters`)
}
